#include "GUI.hpp"
#include "main.hpp"
#include <allegro.h>
#include "button.hpp"
#include "input.hpp"
#include <iostream>


cGUI::cGUI(int x, int y, int w, int h, int bgCol)
{
    pos = new sPoint(x, y);
    size = new sPoint(w, h);
    bgCol = bgCol;
    bufor = create_bitmap(size->x, size->y);
    clear_to_color(bufor, bgCol);

    install_mouse();
    show_mouse(screen);
    unscare_mouse();
}

cGUI::~cGUI()
{
    delete pos;
    delete size;
    destroy_bitmap(bufor);
    for (int i = 0; i < controls.size(); i++)
        delete controls[i];
}

void cGUI::draw()
{
    for (unsigned short i = 0; i < controls.size(); i++)
    {
        controls[i]->draw(bufor);
    }

    blit(bufor, screen, 0, 0, pos->x, pos->y, size->x, size->y);
}

int cGUI::ctrlAdd(short type, int x, int y, int w, int h, char * v, int tCol, int bgCol)
{
    cControl * c;
    bool cr = false;

    switch (type)
    {
        case BUTTON_TYPE:
        {
            controls.push_back(c);
            controls[controls.size()-1] = new cButton(x, y, w, h, v, tCol, bgCol);
            cr = true;
            break;
        }

        case INPUT_TYPE:
        {
            controls.push_back(c);
            controls[controls.size()-1] = new cInput(x, y, w, h, v, tCol, bgCol);
            cr = true;
            break;
        }

        case COMBO_TYPE:
        {
            controls.push_back(c);
            //controls[controls.size()-1] = new cButton(x, y, w, h, v, tCol, bgCol);
            cr = true;
            break;
        }

        case CHECKBOX_TYPE:
        {
            controls.push_back(c);
            //controls[controls.size()-1] = new cButton(x, y, w, h, v, tCol, bgCol);
            cr = true;
            break;
        }
    }

    if (cr) return controls.size() - 1;
    return -1;
}

bool cGUI::ctrlDelete(int id)
{
    if (id < 0 || id >= controls.size()) return false;

    controls.erase(controls.begin() + id);
    return true;
}

bool cGUI::ctrlSetPos(int id, int x, int y)
{
    if (id < 0 || id >= controls.size()) return false;

    controls[id]->pos->x = x;
    controls[id]->pos->y = y;

    return true;
}

bool cGUI::ctrlSetSize(int id, int w, int h)
{
    if (id < 0 || id >= controls.size()) return false;

    controls[id]->size->x = w;
    controls[id]->size->y = h;

    return true;
}

bool cGUI::ctrlHide(int id)
{
    if (id < 0 || id >= controls.size()) return false;

    controls[id]->show = false;
    return true;
}

bool cGUI::ctrlShow(int id)
{
    if (id < 0 || id >= controls.size()) return false;

    controls[id]->show = true;
    return true;
}

bool cGUI::ctrlSetValue(int id, char * v)
{
    if (id < 0 || id >= controls.size()) return false;

    controls[id]->value = v;
    return true;
}

bool cGUI::ctrlSetTextColor(int id, int color)
{
    if (id < 0 || id >= controls.size()) return false;

    controls[id]->textColor = color;
    return true;
}

bool cGUI::ctrlSetBgColor(int id, int color)
{
    if (id < 0 || id >= controls.size()) return false;

    colorToColor(controls[id]->img, bgColor, color);
    controls[id]->bgColor = color;
    return true;
}

bool cGUI::isClicked(int id)
{
    if (id < 0 || id >= controls.size()) return false;
    return controls[id]->isClicked();
}
